﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BackgrounderService.ServiceReference1;

namespace BackgrounderService
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            P = new Properties.Settings();
            timer = new System.Timers.Timer(60000);
            timer.AutoReset = true;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            //upload.OnUploadFinish += new UploadModel.UploadFinishHandler(upload_OnUploadFinish);
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (account != null)
            {
                upload.BeginUpload(account, DataType.Mouse);
                upload.BeginUpload(account, DataType.Key);
                upload.BeginUpload(account, DataType.SysInfo);
            }
        }

        void upload_OnUploadFinish(bool IsSuccess, DataType type, TimeSpan UsedTime, string Infomation)
        {
            MessageBox.Show(IsSuccess.ToString() + " " + UsedTime.ToString() + ' ' + Infomation);
        }

        private System.Timers.Timer timer;
        
        private Properties.Settings P;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.PCHMS.ContextMenu = contextMenu1;
            PCHMS.ShowBalloonTip(3000, "PCHMS", "Application is running backgroud", ToolTipIcon.Info);
            this.textBox1.Text = P.Username;
            this.textBox2.Text = P.Password;
            this.SavePassword.Checked = P.Savepassword;
            this.checkBox2.Checked = P.AutoLogin;

            var ps = pm.GetPlugins();
            foreach (string k in ps.Keys)
            {
                ToolStripMenuItem i = new ToolStripMenuItem(k);
                i.CheckOnClick=true;
                i.Checked = ps[k];
                this.pluginsToolStripMenuItem.DropDownItems.Add(i);
            }
            var ss = pm.GetSettings();
            foreach (string k in ss)
            {
                ToolStripMenuItem i = new ToolStripMenuItem(k);

                this.settingToolStripMenuItem.DropDownItems.Add(i);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
            }
            this.Hide();

        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("aa");
            Application.Exit();
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            if (HookWrap.MouseHookState != HookWrap.HookState.Runing)
            {
                HookWrap.StartMouseHook();
                HookWrap.StartKeyHook();
                HookWrap.StartSysHook();
                if (HookWrap.MouseHookState == HookWrap.HookState.Runing)
                {
                    PCHMS.ShowBalloonTip(3000, "Hook State", "Mouse hook state: " + HookWrap.MouseHookState.ToString() +
                     "\nKey hook state:" + HookWrap.KeyHookState.ToString(), ToolTipIcon.Info);
                    menuItem1.Text = "Stop Hook";
                }
                else
                    PCHMS.ShowBalloonTip(3000, "Error", "Hook Failed", ToolTipIcon.Error);

            }
            else
            {
                HookWrap.StopMouseHook();
                HookWrap.StopKeyHook();
                HookWrap.StopSysHook();
                if (HookWrap.MouseHookState != HookWrap.HookState.Runing)
                {   
                    menuItem1.Text = "Start Hook";
                }
                PCHMS.ShowBalloonTip(3000, "Hook State", "Mouse hook state: " + HookWrap.MouseHookState.ToString() +
                     "\nKey hook state:" + HookWrap.KeyHookState.ToString(), ToolTipIcon.Info);

            }

        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            var ProcessInfo = 
                new System.Diagnostics.ProcessStartInfo("Intel.Client.GUI.exe");
            try
            {
                System.Diagnostics.Process.Start(ProcessInfo);
            }
            catch (System.Exception ex)
            {
            	MessageBox.Show("Analysis  failed!");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //var a = new UploadModel();
            
            //a.OnUploadFinish += delegate(bool IsSuccess, TimeSpan UsedTime, string Infomation)
            //{
            //    System.Windows.Forms.MessageBox.Show(Infomation);
            //};
            //a.BeginUpload(new Account(this.textBox1.Text, this.textBox2.Text));
            //TODO INSERT UPDATA CODE HEER
            if (!timer.Enabled)
            {
                if (this.textBox1.Text == "")
                {
                    MessageBox.Show("Please enter username");
                    return;
                }
                if (this.textBox2.Text == "")
                {
                    MessageBox.Show("Please enter password");
                    return;
                }
                P.Username = this.textBox1.Text;
                P.Save();
                this.textBox1.ReadOnly = true;
                this.textBox2.ReadOnly = true;
                var client = new PCHMSServiceClient();
                bool r;
                try
                {
                    r = client.Validate(textBox1.Text, this.textBox2.Text);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Server Error\n Resion:"+ex.Message);
                    return;
                }
                if (!r)
                {
                    MessageBox.Show("username or password wrong");
                    this.textBox1.ReadOnly = false;
                    this.textBox2.ReadOnly = false;
                    return;
                }
                if (SavePassword.Checked)
                {

                    P.Password = this.textBox2.Text;
                    P.Save();
                }
                account = new Account(this.textBox1.Text, this.textBox2.Text);
                timer.Enabled = true;
                this.button1.Text = "Stop";
            }
            else
            {
                timer.Enabled = false;
                account = null;
                this.textBox1.ReadOnly = false;
                this.textBox2.ReadOnly = false;
                this.button1.Text = "Start";
            }
           
        }

        private void SavePassword_CheckedChanged(object sender, EventArgs e)
        {
           
            P.Savepassword = this.SavePassword.Checked;
            P.Save();
            if (this.SavePassword.Checked == false)
            {
                this.checkBox2.Checked= false;
                P.Password = "";
                this.checkBox2.Checked = false;
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
             P.AutoLogin = checkBox2.Checked;
             P.Save();
             if (checkBox2.Checked == true)
                SavePassword.Checked = true;
        }

        private Account account;

        private PluginManger pm = new PluginManger();

        private UploadModel upload = new UploadModel();

        private void pluginsToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var item = e.ClickedItem as ToolStripMenuItem;
            if (item != null)
            {
                pm.PluginStateChange(item.Text, item.Checked);
            }
        }

        private void settingToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            var item = e.ClickedItem as ToolStripMenuItem;
            if (item != null)
            {
                pm.Setting(e.ClickedItem.Text);
            }
        }

    }
}
